package com.fnzx.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.fnzx.entity.ArticleEntity;
import com.fnzx.entity.ExpertEntity;
import com.fnzx.entity.NasLinkEntity;
import com.fnzx.service.NasLinkService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.R;

import com.fnzx.util.oss.OssUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;



/**
 *
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-10-07 10:54:12
 */
@RestController
@RequestMapping("iot/naslink")
@Api(tags ="nas链接")
public class NasLinkController {
    @Autowired
    private NasLinkService nasLinkService;

    /**
     * 列表
     */
    @ApiOperation(value = "前台页面-分页查询", notes = "查询出所有链接")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页记录数", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/list")
    public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageUtils page = nasLinkService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "前台页面-标题查询",notes = "通过文章标题查询出文章")
    @GetMapping("/info/{title}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "title",value = "文章标题",paramType = "path",required = true,dataType = "String")
    })
    public R info(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page  = nasLinkService.queryPageByTitle(params);
        return R.ok().put("page", page);
    }
    /**
     * 保存
     */
    @ApiOperation(value = "后台页面-添加", notes = "{\"linkTitle\": \"\",\n" +
            "\t\"linkUrl\": \"\"}")
    @PostMapping("/save")
    public R save(@RequestBody NasLinkEntity nasLink,
                      @RequestHeader String token) {
        nasLinkService.save(nasLink);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "后台页面-修改", notes = "{\"id\": 0,\n" +
            "\t\"linkTitle\": \"\",\n" +
            "\t\"linkUrl\": \"\"}")
    @PostMapping("/update")
    public R update(@RequestBody NasLinkEntity nasLinkEntity,
                    @RequestHeader String token) {
        Long id =nasLinkEntity.getId();
        String nasLink=nasLinkService.getById(id).getLinkUrl();
       if(!nasLinkEntity.getLinkUrl().equals(nasLink)){
    OssUtils.delete(nasLink);
     }nasLinkService.updateById(nasLinkEntity);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "后台页面-删除", notes = "删除nas链接，可以同时删除多个")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids,
                    @RequestHeader String token) {
        for (Long id : ids) {
            String nasLink =nasLinkService .getById(id).getLinkUrl();
            OssUtils.delete(nasLink);
        }
        nasLinkService.removeByIds(Arrays.asList(ids));
        return R.ok();
        }

 }
